package bg.btanev.eightyfour.client.view;

import java.util.HashSet;
import java.util.Set;

import bg.btanev.eightyfour.client.presenter.listener.ActionMenuOpenListener;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.BeforeSelectionEvent;
import com.google.gwt.event.logical.shared.BeforeSelectionHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedTabBar;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 *
 * @author B.Tanev
 *
 */
public class TargetListElementsContainerView extends Composite {
	private final DecoratedTabBar actionsMenuTabBar;
	private final VerticalPanel actionDataCenterPanel;
	private final VerticalPanel targetsListElementsContainer;
	private String[] actionMenusText;

	private Set<ActionMenuOpenListener> actionMenuListeners = new HashSet<ActionMenuOpenListener>();

	public TargetListElementsContainerView() {
		final VerticalPanel targetListContainer = new VerticalPanel();
		initWidget(targetListContainer);
		targetListContainer.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		targetListContainer.setWidth("100%");
		targetListContainer.setSpacing(1);

		final DockPanel actionsPanel = new DockPanel();
		actionsPanel.setBorderWidth(1);
		actionsPanel.setWidth("100%");
		actionsPanel.setVisible(false);

		actionsMenuTabBar = new DecoratedTabBar();
		actionsMenuTabBar.setWidth("100%");
		actionsMenuTabBar.addBeforeSelectionHandler(new BeforeSelectionHandler<Integer>() {
			@Override
			public void onBeforeSelection(BeforeSelectionEvent<Integer> event) {
				Integer item = event.getItem();
				if (item != null && actionMenusText != null && actionMenusText.length > 0) {
					int pos = item.intValue();

					if (pos >= 0 && pos < actionMenusText.length) {
						actionsPanel.setVisible(true);
						String tabText = actionMenusText[pos];
						notifyActionMenuOpen(tabText);
					}
				}
			}
		});

		targetListContainer.add(actionsMenuTabBar);
		targetListContainer.add(actionsPanel);

		actionDataCenterPanel = new VerticalPanel();
		actionsPanel.add(actionDataCenterPanel, DockPanel.CENTER);
		actionDataCenterPanel.setSpacing(3);
		actionDataCenterPanel.setWidth("100%");

		HorizontalPanel actionsControlSouthPanel = new HorizontalPanel();
		actionsControlSouthPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		actionsControlSouthPanel.setSpacing(5);
		actionsControlSouthPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);

		actionsPanel.add(actionsControlSouthPanel, DockPanel.SOUTH);
		actionsPanel.setCellHorizontalAlignment(actionsControlSouthPanel, HasHorizontalAlignment.ALIGN_RIGHT);
		actionsControlSouthPanel.setWidth("100%");

		Button hideActionsButton = new Button("Hide target actions panel button");
		hideActionsButton.setText("Hide");
		hideActionsButton.setWidth("67px");
		hideActionsButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				actionsMenuTabBar.selectTab(-1);
				actionsPanel.setVisible(false);
			}
		});
		actionsControlSouthPanel.add(hideActionsButton);

		targetsListElementsContainer = new VerticalPanel();
		targetsListElementsContainer.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
		targetsListElementsContainer.setWidth("100%");
		targetsListElementsContainer.getElement().getStyle().setPadding(5.0, Unit.PX);
		targetListContainer.add(targetsListElementsContainer);
		// targetsListVerticalPanel.setSpacing(2);
	}

	public void addActionMenuListener(ActionMenuOpenListener listener) {
		if (listener != null) {
			actionMenuListeners.add(listener);
		}
	}

	private void notifyActionMenuOpen(String menu) {
		for (ActionMenuOpenListener listener : actionMenuListeners) {
			listener.onOpen(menu);
		}
	}

	public void addActionMenus(String[] actionMenus) {
		this.actionMenusText = actionMenus;
		int oldMenusCount = actionsMenuTabBar.getTabCount();
		for (int i = 0; i < oldMenusCount; i++) {
			actionsMenuTabBar.removeTab(i);
		}

		if (actionMenus != null && actionMenus.length > 0) {
			for (int i = 0; i < actionMenus.length; i++) {
				actionsMenuTabBar.addTab(actionMenus[i]);
			}
		}
	}

	public HasWidgets getElementsContainer() {
		return targetsListElementsContainer;
	}

	public HasWidgets getActionDataCenterPanel() {
		return actionDataCenterPanel;
	}
}
